<?php if (!defined('TL_ROOT')) die('You can not access this file directly!');

/**
 * This class is for online shop order process - payment.
 * params order step
 * This is the children of controller.
 * PHP5 CMS Typolight
 * Copyright is 34n.sam@gmail.com
 */
 
 /**
 * Start page
 */
session_start();

$payment = new Payment();
$payment->run();

class Payment extends Controller
{
	/**
	 * This is class construct function.
	 * @params null
	 * @Return null
	 */
	public function __construct()
	{
		parent::__construct();		
	}
	
	/** 
	 * Start page function
	 * @params null
	 * @Return null
	 */
	public function run()
	{
		// Check shopping cart.
		if (!isset($_SESSION['cart']) || !$_SESSION['cart'])
		{
			$this->redirect('shoppingcart.html');
		}
		
		$this->import('Database');
		$is_return = true;
		if (isset($_POST['address_shipping']) && $_POST['address_shipping'])
		{
			if (isset($_GET['payment'])&&$_GET['payment']=='COD'){
				$obj_shipping = $this->Database->prepare("SELECT `china_province`.`province_en` 
															FROM `tl_address_book` 
															INNER JOIN `china_province` ON `china_province`.`id`=`tl_address_book`.state
															WHERE `tl_address_book`.`id`=?")
												->execute(trim($_POST['address_shipping']));
				if ($obj_shipping->numRows < 1 || $obj_shipping->province_en != 'Shanghai'){
					$is_return = false;
				}
			}
			$_SESSION['ORDER']['address_shipping'] = $_POST['address_shipping'];
		}
		if (isset($_POST['address_billing']) && $_POST['address_billing'])
		{
			$_SESSION['ORDER']['address_billing'] = $_POST['address_billing'];
		}
		if (isset($_POST['shipping']) && $_POST['shipping'])
			$_SESSION['ORDER']['id_carrier'] = $_POST['shipping'];
		
		if (isset($_GET['is_return']) && $_GET['is_return'] == 'validation')
		{
			if ($_POST && $is_return)
				$this->redirect('validation.html?method='.$_GET['payment']);
		}
		
		echo $this->getPaymentlists();
	}
	
	/**
	 * Get all the payment method
	 * @params null
	 * @Return html string.
	 */
	public function getPaymentlists()
	{
		//$is_shanghai_only = false;
		$obj_shipping = $this->Database->prepare("SELECT * FROM `tl_address_book` WHERE `id`=?")
						->execute($_SESSION['ORDER']['address_shipping']);
		/*if ($obj_shipping->country == 'cn'){
			$obj_city = $this->Database->prepare("SELECT * 
												FROM `china_city` 
												LEFT JOIN `china_province` ON `china_province`.`id`=`china_city`.`pid`
												WHERE `china_city`.`id`=?")
										->execute($obj_shipping->city);
			if ($obj_city->province_en == 'Shanghai'){
				$is_shanghai_only = true;
			}
		}*/
		
		if (!$_COOKIE['PAY_CURRENCY']){
			$objSession = $this->Database->prepare("SELECT * FROM tl_session WHERE hash=? AND name=?")
										->execute($_COOKIE['FE_USER_AUTH'], 'FE_USER_AUTH');
			$obj_member = $this->Database->prepare("SELECT * FROM tl_member WHERE id=?")
										->execute($objSession->pid);
			$preferred = ($obj_member->cur)?$obj_member->cur:$_COOKIE['DAFT_CURRENCY'];
			if (!$preferred){
				$obj_currency = $this->Database->prepare("SELECT * FROM `tl_currency` WHERE `is_default`=1")
												->execute(time());
				
				$preferred = $obj_currency->iso_code;
			}
			
			setcookie("PAY_CURRENCY", $preferred);
		}
		if ((!$_COOKIE['PAY_CURRENCY']&&$preferred=='USD') || $_COOKIE['PAY_CURRENCY'] == 'USD'){
			// COD and Paypal payment.
			$strHtml = "<div class=\"payment_module\">
					<!--<input type=\"radio\" name=\"payment\" value=\"\" class=\"radio\" />-->
					<!--<a title=\"Pay with PayPal International\" href=\"javascript:$('#paypal_form').submit();\">-->
					<a title=\"Pay with PayPal International\" href=\"".$this->Environment->base."validation.html?method=Paypal\">
					<img alt=\"Pay with PayPal International\" src=\"images/paypal/paypal.gif\"/>
					<p>All payment will be deducted from your account in US$. </p>
					</a>
					</div>";
		}else{
			$strHtml = (($obj_shipping->country == 'cn') ? "<div class=\"payment_module delivery_cod\">
					<!--<input type=\"radio\" name=\"payment\" value=\"\" class=\"radio\" />-->
					<a title=\"Pay with cash on delivery (COD)\" href=\"".$this->Environment->base."validation.html?method=COD\"><h4>Pay on delivery (China Only)</h4>
					<!--<img style=\"float: left;\" alt=\"Pay with cash on delivery (COD)\" src=\"".$this->Environment->base."images/cashondelivery/cashondelivery.gif\"/>-->
					<p>You pay for the merchandise upon delivery with cash, OR by China Unionpay bank card.</p>
					<!--<p>* Note: Shanghai Only. Please call +86 21 65353451 if you have questions.</p>-->
					</a>
					</div>" : "") . "<div class=\"payment_module\">
					<a title=\"Pay with Alipay\" href=\"".$this->Environment->base."validation.html?method=Alipay\">
					<img alt=\"Pay with Alipay\" src=\"images/alipay/alipay_2.gif\"/>
					<p>All payment will be deducted from your account in RMB. </p>
					</a>
					</div>";
		}
		
		
					
		return $strHtml;
	}
}

?>